package org.gp.Interfaz;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;

import org.gp.tringes.GestorIGInicio;
import org.gp.tringes.GestorIGVenderPerro;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
import java.util.Iterator;
import javax.swing.SwingConstants;


public class IGVenderPerro extends JDialog{
	
	private String numFederado;
	private String numPerro;
	
	private JPanel panel;
	private JPanel panel_1;
	private JPanel panel_2;
	private JLabel lblTitulo;
	private JComboBox comboVendedores;
	private JLabel lblAviso;
	private JTextField txtPrecio;
	private JButton btnAceptar;
	private JButton btnCancelar;
	
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					IGVenderPerro dialog = new IGVenderPerro("1234567","");
					dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
					dialog.setVisible(true);
					dialog.setModal(true);
					dialog.setResizable(false);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	public IGVenderPerro(String pNumFederado, String pNumPerro) {
		this.numFederado = pNumFederado;
		this.numPerro = pNumPerro;
		
		setBounds(100, 100, 250, 175);
		//ID-199
		setTitle(GestorIGInicio.getGestorIGInicio().getTexto(199));
		
		getContentPane().setLayout(new BorderLayout(0, 0));
		getContentPane().add(getPanel(), BorderLayout.NORTH);
		getContentPane().add(getPanel_1(), BorderLayout.CENTER);
		getContentPane().add(getPanel_2(), BorderLayout.SOUTH);
		
	}
	
	private JPanel getPanel(){
		if(panel == null){
			panel = new JPanel();
			panel.add(getLblTitulo());
		}
		return panel;
	}
	private JLabel getLblTitulo() {
		if (lblTitulo == null) {
			//ID-200
			lblTitulo = new JLabel(GestorIGInicio.getGestorIGInicio().getTexto(200));
		}
		return lblTitulo;
	}
	
	private JPanel getPanel_1(){
		if(panel_1 == null){
			panel_1 = new JPanel();
			panel_1.setLayout(null);
			panel_1.add(getComboVendedores());
			panel_1.add(getLblAviso());
			panel_1.add(getTxtPrecio());
		}
		return panel_1;
	}
	private JComboBox getComboVendedores() {
		if (comboVendedores == null) {
			comboVendedores = new JComboBox();
			comboVendedores.setBounds(50, 5, 135, 20);
			Iterator<String> lista = GestorIGVenderPerro.getGestorIGVenderPerro().getDuenos(numFederado);
			while(lista.hasNext()) {
				String camp = lista.next();
				comboVendedores.addItem(camp);
			}
			comboVendedores.setSelectedIndex(0);
		}
		return comboVendedores;
	}
	private JLabel getLblAviso() {
		if (lblAviso == null) {
			//ID-201
			lblAviso = new JLabel(GestorIGInicio.getGestorIGInicio().getTexto(201));
			lblAviso.setHorizontalAlignment(SwingConstants.CENTER);
			lblAviso.setBounds(45, 30, 145, 15);
		}
		return lblAviso;
	}
	private JTextField getTxtPrecio() {
		if (txtPrecio == null) {
			txtPrecio = new JTextField();
			txtPrecio.setBounds(74, 55, 86, 20);
			txtPrecio.setColumns(10);
		}
		return txtPrecio;
	}
	
	private JPanel getPanel_2(){
		if(panel_2 == null){
			panel_2 = new JPanel();
			panel_2.add(getBtnAceptar());
			panel_2.add(getBtnCancelar());
		}
		return panel_2;
	}
	private JButton getBtnAceptar() {
		if (btnAceptar == null) {
			//ID-2
			btnAceptar = new JButton(GestorIGInicio.getGestorIGInicio().getTexto(2));
			btnAceptar.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent arg0) {
					try{
						Boolean existePerro = GestorIGVenderPerro.getGestorIGVenderPerro().existeVenta(numPerro);
						if(!existePerro){
							//ID-202 y 4
							int opcion = JOptionPane.showConfirmDialog(null, GestorIGInicio.getGestorIGInicio().getTexto(202), GestorIGInicio.getGestorIGInicio().getTexto(4), JOptionPane.YES_NO_OPTION);
							if(opcion == 0){
								Integer precio = Integer.valueOf(txtPrecio.getText());
								GestorIGVenderPerro.getGestorIGVenderPerro().anadirVenta(numFederado, (String)comboVendedores.getSelectedItem(), numPerro, precio);
								//ID-68 y 6
								JOptionPane.showMessageDialog(null, GestorIGInicio.getGestorIGInicio().getTexto(68), GestorIGInicio.getGestorIGInicio().getTexto(6), JOptionPane.INFORMATION_MESSAGE);
								dispose();
							}
						}else{
							//ID-203 y 4
							JOptionPane.showMessageDialog(null, GestorIGInicio.getGestorIGInicio().getTexto(203), GestorIGInicio.getGestorIGInicio().getTexto(4), JOptionPane.WARNING_MESSAGE);
						}
					} catch (SQLException e) {
					e.printStackTrace();
					}
				}
			});
		}
		return btnAceptar;
	}
	private JButton getBtnCancelar() {
		if (btnCancelar == null) {
			//ID-8
			btnCancelar = new JButton(GestorIGInicio.getGestorIGInicio().getTexto(8));
			btnCancelar.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent arg0) {
					dispose();
				}
			});
		}
		return btnCancelar;
	}
}
